<?php

/**
 * Description of Comment
 *
 * @author Vu
 */
 
class Comment extends Zend_Db_Table_Abstract {
	
	protected $_name = 'cms_comment';
    protected $_primary = 'comment_id';
    
	public function getAllComments($page = 1, $order = "c.created desc", $filter = "") {
        $tb = Zend_Db_Table::getDefaultAdapter();
        $tb->setFetchMode(Zend_Db::FETCH_OBJ);
        
        $select = $tb->select()
        					->from(array('c' => 'cms_comment'))
        					->join(array('d' => 'cms_deal'), 'c.deal_id = d.deal_id', 'd.deal_title')
        					->where("c.content LIKE '%$filter%' OR d.deal_title LIKE '%$filter%'")
        					->order($order);
            
        $paginator = Zend_Paginator::factory($select);
    	$paginator->setItemCountPerPage(Zend_Registry::get('page_size'));
    	$paginator->setCurrentPageNumber($page); 
    	
        return $paginator;
    }
    
	public function getCommentById($commentid){
        $row = $this->fetchRow($this->select()->where('comment_id=?', $commentid));
        
        return $row;
    }
    
    public function getAllCommentByDealId($dealid) {
    	$tb = Zend_Db_Table::getDefaultAdapter();
        $tb->setFetchMode(Zend_Db::FETCH_OBJ);
        
        $select = $tb->select()->from(array('c' => 'cms_comment'))
        						->join(array('u' => 'user_account'), 'c.user_id = u.user_id')
        						->where('deal_id=? AND status!=0', $dealid)->order('created DESC');
        $rows = $tb->fetchAll($select);
    	
        return $rows;
    }
    
	public function setPublicComment($commentid) {
    	$tb = Zend_Db_Table::getDefaultAdapter();
        $tb->setFetchMode(Zend_Db::FETCH_OBJ);
        $comment = $this->getCommentById($commentid);
        $comment->status = 1;
        $comment->save();
    }
    
	public function setPrivateComment($commentid) {
    	$tb = Zend_Db_Table::getDefaultAdapter();
        $tb->setFetchMode(Zend_Db::FETCH_OBJ);
        $comment = $this->getCommentById($commentid);
        $comment->status = 0;
        $comment->save();
    }
}